package com.lbt.eduservice.controller;


import com.lbt.commonuntils.R;
import com.lbt.eduservice.client.VodClient;
import com.lbt.eduservice.entity.EduVideo;
import com.lbt.eduservice.service.EduVideoService;
import com.lbt.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-02
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/video")
public class EduVideoController {
    @Autowired
    EduVideoService videoService;
    @Autowired
    VodClient vodClient;
    @ApiOperation(value ="删除视频 删除小节" )
    @DeleteMapping("/{videoId}")
    public R deleteVideo(@PathVariable String videoId){
        //根据小节 Id 得到在oss上面的视频ID 根据ID 删除 阿里云上面的视频
        EduVideo video = videoService.getById(videoId);
        String sourceId = video.getVideoSourceId();
        //如果阿里云上面没有传入视频则不需要删除
        if(!StringUtils.isEmpty(sourceId)){
            //调用 服务之间调用
            R result = vodClient.removeVideoByID(sourceId);
            if(result.getCode()==20001){
                throw new GuliException(20001,"服务请求超时,熔断器");
            }
        }
        videoService.removeById(videoId);
        return R.ok();
    }

    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo video){
        boolean falg = videoService.save(video);
        if(falg){
            return R.ok();
        }
        return R.error();
    }
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo video){
        boolean falg = videoService.updateById(video);
        if(falg){
            return R.ok();
        }
        return R.error();
    }


}

